package com.wanxin.wb.common.support.json;

/**
 * feiniu.com Inc.
 * Copyright (c) 2013-2015 All Rights Reserved.
 */


import com.alibaba.fastjson.serializer.PropertyFilter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <B>Description:</B>包含字段的PropertyFilter<br>
 * <B>Create on:</B> 2015年4月1日 下午5:31:16<br>
 *
 * @author 
 * @version 1.0
 */
public class IncludePropertyFilter implements PropertyFilter {
    private Class        clazz;                   //排除的类
    private List<String> fields = new ArrayList(); //包含的字段集合

    public IncludePropertyFilter(Class clazz, String... fields) {
        this.clazz = clazz;
        if (fields != null) {
            this.fields.addAll(Arrays.asList(fields));
        }
    }

    public IncludePropertyFilter(String... fields) {
        this(null, fields);
    }

    public IncludePropertyFilter(Class clazz, List<String> fields) {
        this.clazz = clazz;
        if (fields != null) {
            this.fields.addAll(fields);
        }
    }

    public IncludePropertyFilter(List<String> fields) {
        this(null, fields);
    }

    /**
     * <B>Description:</B> <br>
     * <B>Create on:</B> 2015年4月1日 下午5:31:16<br>
     * @author 
     * @see com.alibaba.fastjson.serializer.PropertyFilter#apply(Object, String, Object)
     */
    @Override
    public boolean apply(Object object, String name, Object value) {
        if (this.fields == null || this.fields.isEmpty())
            return true;
        if (object == null)
            return true;
        if (clazz == null) {
            return fields.contains(name);
        } else if (clazz.equals(object.getClass())) {
            return fields.contains(name);
        }
        return true;
    }

}
